#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ControlDTCSetting_0x85_test.py
@Time    :   2022/08/29 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

controlDTCSetting = [([0x85, 0x01], [0xC5, 0X01]),
                     ([0x85, 0x02], [0xC5, 0X02])
                     ]

controlDTCSetting_SuppressPosMsgBit = [ ([0x85, 0x81], []),
                                        ([0x85, 0x82], [])
                                      ]

controlDTCSetting_SuppressPosMsgBit_NRC13 = [   ([0x85, 0x82, 0x01], [0x7F, 0x85, 0x13]),
                                                ([0x85, 0x81, 0xFF], [0x7F, 0x85, 0x13]),
                                                ([0x85, 0x82, 0x13], [0x7F, 0x85, 0x13]),
                                            ]

controlDTCSetting_SuppressPosMsgBit_NRC12 = [   ([0x85, 0x80], [0x7F, 0x85, 0x12]),
                                                ([0x85, 0x83], [0x7F, 0x85, 0x12]),
                                                ([0x85, 0x3F], [0x7F, 0x85, 0x12]),
                                                ([0x85, 0x40], [0x7F, 0x85, 0x12]),
                                                ([0x85, 0x5F], [0x7F, 0x85, 0x12]),
                                                ([0x85, 0x60], [0x7F, 0x85, 0x12]),
                                                ([0x85, 0x7E], [0x7F, 0x85, 0x12]),
                                                ([0x85, 0x7F], [0x7F, 0x85, 0x12])
                                            ]


controlDTCSetting_NRC13 = [([0x85], [0x7F, 0x85, 0x13]),
                            ([0x85, 0x02, 0x01], [0x7F, 0x85, 0x13]),
                            ([0x85, 0x01, 0xFF], [0x7F, 0x85, 0x13]),
                            ([0x85, 0x22, 0x13], [0x7F, 0x85, 0x13]),
                             ]

controlDTCSetting_NRC12 = [ ([0x85, 0x00], [0x7F, 0x85, 0x12]),
                            ([0x85, 0x03], [0x7F, 0x85, 0x12]),
                            ([0x85, 0x3F], [0x7F, 0x85, 0x12]),
                            ([0x85, 0x40], [0x7F, 0x85, 0x12]),
                            ([0x85, 0x5F], [0x7F, 0x85, 0x12]),
                            ([0x85, 0x60], [0x7F, 0x85, 0x12]),
                            ([0x85, 0x7E], [0x7F, 0x85, 0x12]),
                            ([0x85, 0x7F], [0x7F, 0x85, 0x12])
                         ]

controlDTCSetting_NRC7F = [ ([0x85, 0x01], [0x7F, 0X85, 0x7F]),
                            ([0x85, 0x02], [0x7F, 0X85, 0x7F]),
                            ([0x85, 0x81], [0x7F, 0X85, 0x7F]),
                            ([0x85, 0x82], [0x7F, 0X85, 0x7F])
                          ]


class Test_ControlDTCSetting_Phy(object):

    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting]
    @allure.severity('normal')
    @allure.story('Control DTC Setting Test')
    @allure.title('Test positive response of control dtc setting 0x85')
    @allure.description('Test positive response of 0x85')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting, ids=ids)
    def test_ControlDTCSetting_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)


    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_NRC13]
    @allure.severity('minor')
    @allure.title('Test negative response NRC13 of control dtc setting 0x85')
    @allure.description('Test negative response code of 0x28 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_NRC13, ids=ids)
    def test_ControlDTCSetting_NRC13_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_NRC12]
    @allure.severity('minor')
    @allure.title('Test negative response NRC12 of control dtc setting 0x85')
    @allure.description('Test negative response code of 0x28 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_NRC12, ids=ids)
    def test_ControlDTCSetting_NRC12_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)
        

    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_SuppressPosMsgBit]
    @allure.severity('normal')
    @allure.title('Test positive response of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test positive response of 0x85  when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_SuppressPosMsgBit, ids=ids)
    def test_ControlDTCSetting_SuppressPosMsgBit_Phy(self, action, Driver,req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)



    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_SuppressPosMsgBit_NRC13]
    @allure.severity('minor')
    @allure.title('Test negative response NRC13 of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test negative response code of 0x85 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_SuppressPosMsgBit_NRC13, ids=ids)
    def test_ControlDTCSetting_SuppressPosMsgBit_NRC13_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_SuppressPosMsgBit_NRC12]
    @allure.severity('minor')
    @allure.title('Test negative response NRC12 of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test negative response code of 0x85 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_SuppressPosMsgBit_NRC12, ids=ids)
    def test_ControlDTCSetting_SuppressPosMsgBit_NRC12_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    @allure.severity('critical')
    @allure.title('Test positive response of control dtc setting 0x85 on off')
    @allure.description('Test dtc code output or not when setting 0x85 on off')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ControlDTCSetting_onoff_Extended_Phy(self, action, Driver):

        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 3, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)

        # 判断电压过高故障是否存在
        a, b, c = Driver['UDS'].search_DTC([0x19, 0x02, 0x09], [0xAC, 0xCE, 0x17, 0x09], timeout=1)
        with assume:
            assert a == False

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x85, 0x02], [0xC5, 0x02], 0, iStep, False)

        # 设置高电压，产生高压DTC
        Driver['power'].set_vltage(17)
        time.sleep(0.5)
        Driver['power'].Read_Measure()
        time.sleep(1)

        a, b, c = Driver['UDS'].search_DTC([0x19, 0x02, 0x09], [0xAC, 0xCE, 0x17, 0x09], timeout=1)
        with assume:
            assert a == False

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x85, 0x01], [0xC5, 0x01], 0, iStep, False)

        a, b, c = Driver['UDS'].search_DTC([0x19, 0x02, 0x09], [0xAC, 0xCE, 0x17, 0x09])
        with assume:
            assert a == True

        Driver['power'].set_vltage(12)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)

    @allure.severity('critical')
    @allure.title('Test positive response of control dtc setting 0x85 SuppressPosMsgBit on off')
    @allure.description('Test dtc code output or not when setting 0x85 SuppressPosMsgBit on off')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ControlDTCSetting_onoff_SuppressPosMsgBit_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 3, iStep, False)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)

        a, b, c = Driver['UDS'].search_DTC([0x19, 0x02, 0x09], [0xAC, 0xCF, 0x16, 0x09], timeout=1)
        with assume:
            assert a == False
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x85, 0x82], [], 0, iStep, False)

        Driver['power'].set_vltage(8.8)
        time.sleep(0.5)
        Driver['power'].Read_Measure()
        time.sleep(1)

        a, b, c = Driver['UDS'].search_DTC([0x19, 0x02, 0x09], [0xAC, 0xCF, 0x16, 0x09], timeout=1)
        with assume:
            assert a == False

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x85, 0x81], [], 0, iStep, False)

        a, b, c = Driver['UDS'].search_DTC([0x19, 0x02, 0x09], [0xAC, 0xCF, 0x16, 0x09])
        with assume:
            assert a == True

        Driver['power'].set_vltage(12)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)


    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_NRC7F]
    @allure.severity('minor')
    @allure.title('Test NRC7F of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test NRC7F of 0x85 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_NRC7F, ids=ids)
    def test_ControlDTCSetting_NRC7F_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    # @allure.severity('minor')
    # @allure.title('Test NRC7F of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    # @allure.description('Test NRC7F of 0x85 when suppressPosMsgBit is enabled with physical addressing')
    # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    # @pytest.mark.parametrize('req,resp', controlDTCSetting_NRC7F, ids=ids)
    # def test_ControlDTCSetting_NRC7F_Programming_Phy(self, action, Driver, req, resp):
    #     iStep = 1
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)


class Test_ControlDTCSetting_Fun(object):

    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting]
    @allure.severity('normal')
    @allure.story('Control DTC Setting Test')
    @allure.title('Test positive response of control dtc setting 0x85')
    @allure.description('Test positive response of 0x85')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting, ids=ids)
    def test_ControlDTCSetting_Extended_Fun(self, action, Driver,req, resp):
        iStep = 1
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun')



    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_NRC13]
    @allure.severity('minor')
    @allure.title('Test negative response NRC13 of control dtc setting 0x85')
    @allure.description('Test negative response code of 0x85 when suppressPosMsgBit is enabled with function addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_NRC13, ids=ids)
    def test_ControlDTCSetting_NRC13_Fun(self, action, Driver, req,resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 3, iStep, False, 'UDSFun', 'noClearFault')

    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_NRC12]
    @allure.severity('minor')
    @allure.title('Test negative response NRC12 of control dtc setting 0x85')
    @allure.description('Test negative response code of 0x85 when suppressPosMsgBit is enabled with function addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_NRC12, ids=ids)
    def test_ControlDTCSetting_NRC12_Fun(self, action, Driver, req,resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 3, iStep, False, 'UDSFun', 'noClearFault')



    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_SuppressPosMsgBit]
    @allure.severity('normal')
    @allure.title('Test positive response of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test positive response of 0x85  when suppressPosMsgBit is enabled with function addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_SuppressPosMsgBit, ids=ids)
    def test_ControlDTCSetting_SuppressPosMsgBit_Fun(self, action, Driver,req,resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')



    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_SuppressPosMsgBit_NRC13]
    @allure.severity('minor')
    @allure.title('Test negative response NRC13 of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test negative response code of 0x85 when suppressPosMsgBit is enabled with function addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_SuppressPosMsgBit_NRC13, ids=ids)
    def test_ControlDTCSetting_SuppressPosMsgBit_NRC13_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 3, iStep, False, 'UDSFun', 'noClearFault')

    
    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_SuppressPosMsgBit_NRC12]
    @allure.severity('minor')
    @allure.title('Test negative response NRC12 of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test negative response code of 0x85 when suppressPosMsgBit is enabled with function addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_SuppressPosMsgBit_NRC12, ids=ids)
    def test_ControlDTCSetting_SuppressPosMsgBit_NRC12_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 3, iStep, False, 'UDSFun', 'noClearFault')

    ids = ['{0}'.format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in controlDTCSetting_NRC7F]
    @allure.severity('minor')
    @allure.title('Test NRC7F of control dtc setting 0x85 when suppressPosMsgBit is enabled')
    @allure.description('Test NRC7F of 0x85 when suppressPosMsgBit is enabled with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp', controlDTCSetting_NRC7F, ids=ids)
    def test_ControlDTCSetting_NRC7F_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 1, iStep, False, 'UDSFun', 'noClearFault')
